package com.example.ssmxuankeback.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

@Slf4j
@RequiredArgsConstructor
@Component
public class FillMetaObjectHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (metaObject.findProperty("createTime", true) != null) {
            // 创建时间
            this.setFieldValByName("createTime", LocalDateTime.now(), metaObject);
        }
        if (metaObject.findProperty("updateTime", true) != null) {
            // 创建时间
            this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        if (metaObject.findProperty("updateTime", true) != null) {
            // 创建时间
            this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        }
    }
}
